// pages/Add_device/Add_device.js
import api from "../../Sendingmethod/Sendingmethod"
import areaList from '../../utils/area.js'
import util from '../../utils/util'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        inputarr: [{
                mobile: '',
            },
            {
                consignee: '',
            },
            {
                address: '',
            }
        ],
        typearr: [],
        //非空判断
        textblen: false,
        //控制弹窗
        ctrlblen: false,
        //控制地址弹窗
        showon: false,
        neirong: '',
        neirong_1: '',
        columns: [1, 1],
        //省
        province: "",
        //市
        city: "",
        //区
        district: '',
        checked: false,
        is_default: 0

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options, '传值');
        this.gettype()

        console.log(areaList);
        this.setData({
            Areaarr: areaList
        })
        console.log(this.data.Areaarr, '赋值后的对象');
    },
    /**
     * 地图选择后
     */
    aer_confirmon(e) {
        console.log(e, '当前地址数据');
        // let data = "inputarr[2].address"
        let province = e.detail.values[0]?.name || ""
        let city = e.detail.values[1]?.name || ""
        let district = e.detail.values[2]?.name || ""
        let text
        if (e.detail.values.length > 0) {
            console.log('走这里');
            text = e.detail.values.map((i) => i?.name || "").join('/')
        } else {
            text = ""
        }
        this.setData({
            showon: false,
            neirong_1: text,
            province,
            city,
            district
        })

        this.feikong()
        console.log('触发关闭');
    },
    /**
     * 关闭
     */
    aer_cancel() {
        this.setData({
            showon: false
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },
    /**
     * 类型选择
     */
    selectype() {

    },
    /**
     * 获取类型
     */
    async gettype() {
        let res = await api.devicetype()
        console.log(res);
        if (res.code == 0) {
            console.log(res, '设备类型');
            let newarr = res.data.map((item) => {
                return {
                    id: item.id,
                    text: item.name
                }
            })
            newarr.unshift({
                id: "",
                text: ""
            });
            this.setData({
                typearr: newarr
            })
        }
    },
    /**
     * 完成事件
     */
    confirm(e) {
        console.log('完成', e);
        let _that = this

        function datafun(data) {
            console.log(data, '触发');
            let result = _that.data.typearr.filter((i) => i.id == data)
            console.log(result, '结果');
            return result[0].text
        }
        let data = `inputarr[0].deviceType`
        this.setData({
            [data]: e.detail.value.id,
            neirong: datafun(e.detail.value.id),
            ctrlblen: false,
            deviceType: e.detail.value.id,
            province: this.data.province,
            city: this.data.city,
            district: this.data.district
        })

        console.log(this.data.inputarr, this.data.neirong);
        this.feikong()
    },
    /**
     *输入事件
     */
    shuru(e) {
        console.log(e);
        //quantity
        let data = "inputarr[1].consignee"
        this.setData({
            [data]: e.detail.value
        })
        this.feikong()
    },
    /**
     *输入事件 电话
     */
    shuru1(e) {
        console.log(e);
        //quantity
        let data = "inputarr[0].mobile"
        this.setData({
            [data]: e.detail.value
        })
        this.feikong()
    },
    /**
     *输入详细地址
     */
    shuru2(e) {
        console.log(e);
        //quantity
        let data = "inputarr[2].address"
        this.setData({
            [data]: e.detail.value
        })
        this.feikong()
    },

    /**
     * 
     * 非空判断
     */
    feikong() {
        // let arr=th
        // 统计非空对象的数量
        let _that = this

        function countEmptyObjects() {
            return _that.data.inputarr.filter(obj => Object.values(obj).every(value => value === '')).length;
        }
        let data = countEmptyObjects()
        if (data == 0) {
            console.log('放行');
            this.setData({
                textblen: true
            })

        } else {
            console.log('未通过');
            this.setData({
                textblen: false
            })
        }
        console.log(data, this.data.inputarr, '当前数据');


    },
    /**
     *  变化
     */
    ckonChange() {
        let blen = !this.data.checked
        this.setData({
            checked: blen,
            is_default: blen ? 1 : 0
        })
    },
    /**
     * 提交申请
     */
    async dianji() {
        let obj
        let _that = this
        wx.showLoading({
            mask: true
        })

        function mergeArrayToObject(arr) {
            return _that.data.inputarr.reduce((acc, obj) => {
                return {
                    ...acc,
                    ...obj
                };
            }, {});
        }
        obj = mergeArrayToObject()
        console.log({
            ...obj,
            is_default: this.data.is_default,
            province: this.data.province,
            city: this.data.city,
            district: this.data.district
        }, '传输数据');

        try {
            let res = await api.myaddressAdd({
                ...obj,
                is_default: this.data.is_default,
                province: this.data.province,
                city: this.data.city,
                district: this.data.district
            })

            if (res.code == 0) {
                wx.showToast({
                    title: '提交成功',
                    icon: 'success',
                    duration: 2000
                })
                console.log(res, );
                wx.hideLoading()
                wx.navigateBack(1)
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'error',
                    duration: 2000
                })
                wx.hideLoading()
            }


        } catch (error) {
            wx.showToast({
                title: '请求失败',
                icon: "error",
                duration: 2000
            })
        }



        console.log(this.data.inputarr);
    },
    tijiaofagdu: util.throttle(function () {
        this.dianji()
    },1000),
    /**
     * 取消
     */
    cancel(e) {
        console.log('取消', e);
        this.setData({
            ctrlblen: false
        })
    },
    /**
     * 变化
     */
    change(e) {
        console.log('变化', e);

    },

    /**
     * 打开弹出
     */
    opening() {
        this.setData({
            ctrlblen: true,

        })

    },
    opening1() {
        this.setData({
            showon: true
        })

    },
    /**
     * inputbox
     */



    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})